package com.lagou.homework.merge;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.BytesWritable;
import org.apache.hadoop.io.SequenceFile;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.io.compress.SnappyCodec;
import org.apache.hadoop.mapreduce.Job;
import org.apache.hadoop.mapreduce.lib.input.FileInputFormat;
import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;
import org.apache.hadoop.mapreduce.lib.output.SequenceFileOutputFormat;

import java.io.IOException;

public class MergeDriver {
    public static void main(String[] args) throws IOException, ClassNotFoundException, InterruptedException {
        Configuration configuration = new Configuration();
        Job job = Job.getInstance(configuration, "MergeDriver");

        // 1.设置 jar包本地路径
        job.setJarByClass(MergeDriver.class);

        // 3.使用的 mapper 和 reducer
        job.setMapperClass(MergeMapper.class);
        job.setReducerClass(MergeReducer.class);
        // 4.map的输出kv类型
        job.setMapOutputKeyClass(Text.class);
        job.setMapOutputValueClass(BytesWritable.class);
        // 5.设置 reduce 输出
        job.setOutputKeyClass(Text.class);
        job.setOutputValueClass(BytesWritable.class);

        // 设置使用自定义 InputFormat 读取数据
        job.setInputFormatClass(MergeInputFormat.class);

        // 指定输出使用的 outputFormat
        job.setOutputFormatClass(SequenceFileOutputFormat.class);

        // 读取的数据路径
        FileInputFormat.setInputPaths(job,new Path("/Users/sumscope/Desktop/lagou-home-work/code/Hadoop框架核心作业/SortFileNum/file"));
        FileOutputFormat.setOutputPath(job,new Path("/Users/sumscope/Desktop/lagou-home-work/code/Hadoop框架核心作业/SortFileNum/file/output"));

        // 提交任务
        boolean b = job.waitForCompletion(true);
        System.exit(b == true ? 0 : 1);
    }
}
